//
//  WBStatusViewModel.swift
//  WeiBoApp
//
//  Created by CrazyHacker on 16/7/2.
//  Copyright © 2016年 CrazyHacker. All rights reserved.
//

import Foundation

class WBStatusViewModel {
    /// 微博数据模型数组
    lazy var statusList = [WBHomeInfoModel]()
    /// 上拉舒心最大错误次数
    var maxFalsePulluoTimes = 3
    var pullupFalseTimes = 0
    
    func loadStatusList(pullup: Bool, completion:(isSuccess: Bool, isShouldRefresh:Bool)->()) {
        
        
        // 下拉刷新
        let since_id = pullup ? 0 : (statusList.first?.id ?? 0)
        // 上拉刷新
        let max_id = !pullup ? 0 : (statusList.last?.id ?? 0)
        if pullup && pullupFalseTimes > maxFalsePulluoTimes {
            completion(isSuccess: true, isShouldRefresh: false)
            return
        }
        
        WebNetworkTools.shared.statusList(since_id: since_id, max_id: max_id) {(list, isSuccess) in
           
            
            guard let array = NSArray.yy_modelArray(with: WBHomeInfoModel.self, json: list ?? []) as? [WBHomeInfoModel] else {
                completion(isSuccess: isSuccess, isShouldRefresh: false)
                return
            }
            
            print("刷新了\(array.count)条数据")
            if pullup {
                self.statusList += array
            } else {
                self.statusList = array + self.statusList
            }
            if pullup && array.count == 0 {
                self.pullupFalseTimes += 1
                completion(isSuccess: isSuccess, isShouldRefresh: false)
            } else {
                completion(isSuccess: isSuccess, isShouldRefresh: true)
            }
        }
        
    }

}
